package org.dtrd.modules.doctor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.doctor.entity.bean.StatisticsDoctorPatientDetail;
import org.dtrd.modules.doctor.entity.po.DtrdRlStatisticsDoctorPatient;
import org.dtrd.modules.doctor.mapper.DtrdRlStatisticsDoctorPatientMapper;
import org.dtrd.modules.doctor.service.IDtrdRlStatisticsDoctorPatientService;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * <p>
 * 医生关联患者统计数据 服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-06-14
 */
@DS("multi-datasource1")
@Service
public class DtrdRlStatisticsDoctorPatientServiceImpl extends ServiceImpl<DtrdRlStatisticsDoctorPatientMapper, DtrdRlStatisticsDoctorPatient> implements IDtrdRlStatisticsDoctorPatientService {

    @Override
    public StatisticsDoctorPatientDetail getStatisticsDoctorPatient(Integer dataId) {
        DtrdRlStatisticsDoctorPatient dtrdRlStatisticsDoctorPatient = getById(dataId);
        return Optional.ofNullable(dtrdRlStatisticsDoctorPatient)
                .map(po -> new StatisticsDoctorPatientDetail().parseFromPo(po))
                .orElse(null);
    }

    @Override
    public boolean saveOrUpdateStatisticsDoctorPatient(StatisticsDoctorPatientDetail dto) {
        DtrdRlStatisticsDoctorPatient dtrdRlStatisticsDoctorPatient = new DtrdRlStatisticsDoctorPatient();
        if (dto.getDataId() == null) {
            dtrdRlStatisticsDoctorPatient = dtrdRlStatisticsDoctorPatient.init();
        }
        dtrdRlStatisticsDoctorPatient.parseFromDto(dto);
        return saveOrUpdate(dtrdRlStatisticsDoctorPatient);
    }

    @Override
    public boolean removeStatisticsDoctorPatient(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public void onConcernChange(Integer doctorId, Integer count) {

    }

    private LambdaQueryWrapper<DtrdRlStatisticsDoctorPatient> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdRlStatisticsDoctorPatient.class)
                .eq(DtrdRlStatisticsDoctorPatient::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdRlStatisticsDoctorPatient> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdRlStatisticsDoctorPatient.class)
                .eq(DtrdRlStatisticsDoctorPatient::getIsDel, 0);
    }
}
